package aa;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Servers {
      private ServerSocket serverSocket;
      private Map<String,PrintWriter> allOut = new HashMap<>();
      public Servers(){
          try {
              System.out.println("正在启动服务端");
              serverSocket=new ServerSocket(9955);
              System.out.println("服务端启动完毕");
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      public void start(){
          try {
             while (true){
                 System.out.println("正在等待客户端连接...");
                 Socket socket = serverSocket.accept();
                 System.out.println("一个客户端连上了");
                 ClientsHandler clientsHandler = new ClientsHandler(socket);
                 Thread t = new Thread(clientsHandler);
                 t.start();

             }

          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      private class ClientsHandler implements Runnable{
          private Socket socket;
          private String host;
          private String nickName;
          public ClientsHandler(Socket socket){
              this.socket=socket;
              host=socket.getInetAddress().getHostAddress();
          }

          public void run(){
              PrintWriter pw=null;
              try {
                  InputStream in = socket.getInputStream();
                  InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                  BufferedReader br = new BufferedReader(isr);
                  String message;
                  OutputStream out = socket.getOutputStream();
                  OutputStreamWriter osw = new OutputStreamWriter(out,StandardCharsets.UTF_8);
                  BufferedWriter bw = new BufferedWriter(osw);
                  pw = new PrintWriter(bw,true);

                  nickName=br.readLine();
                  allOut.put(nickName,pw);
                  sendMessageToAll("上线了,当前在线人数:"+allOut.size());
                  while ((message=br.readLine())!=null){
                      if (message.startsWith("@")){
                          sendMessageToSomeone(message);
                      }else {
                          sendMessageToAll("说:"+message);
                      }
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }finally {
                  allOut.remove(nickName);
                  sendMessageToAll("下线了,当前在线人数:"+allOut.size());
                  try {
                      socket.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
          public void sendMessageToAll(String message){
              System.out.println(nickName+"["+host+"]"+message);
              for (PrintWriter o : allOut.values()){
                  o.println(nickName+"["+host+"]"+message);
              }
          }
          public void sendMessageToSomeone(String message){
              if (message.matches("@.+:.+")){
                  String toNickName = message.substring(1,message.indexOf(":"));
                  if (allOut.containsKey(toNickName)){
                      String toMessage = message.substring(message.indexOf(":")+1);
                      PrintWriter pw = allOut.get(toNickName);
                      pw.println(nickName+"悄悄对你说:"+toMessage);
                  }else {
                      PrintWriter pw = allOut.get(nickName);
                      pw.println("用户["+toNickName+"]不在线!");
                  }
              }else {
                  PrintWriter pw = allOut.get(nickName);
                  pw.println("私聊格式不正确!");
              }
          }
      }

    public static void main(String[] args) {
        Servers servers = new Servers();
        servers.start();
    }

}
